# encoding: utf-8

require 'rubygems'
require 'test/unit'
require File.dirname(__FILE__) + '/../../../lib/capevaluation'
require File.dirname(__FILE__) + '/../../../lib/capevaluation/business/example'
require File.dirname(__FILE__) + '/../../../lib/capevaluation/business/prediction'
require File.dirname(__FILE__) + '/../../../lib/capevaluation/business/crontab'



class Test_Example < Test::Unit::TestCase

  include Capevaluation::Business::Example
  include Capevaluation::Business::Prediction
  include Capevaluation::Business::Crontab




  def setup
    @config = CaiQiu::Support::Config::get_json(File.dirname(__FILE__) + '/../../../etc/ws.json')
    CaiQiu::Core::Launch::load_config(@config)
    @logger = CaiQiu::Support::Log::instance(@config['ws']['log'])
    @redis  = CaiQiu::Support::Redis::instance
    CaiQiu::WebService::CaiQiuSinatra.init(@config['ws'], 5678)
  end

  def teardown
  end

  def test_001_sample
    params = {
        :cmd => 'example',
        :foo => 'bar'
    }
    params = JSON.parse(JSON.generate(params))
    resp   = self.send(params['cmd'], params)
    puts resp.inspect
  end


  def test_002_add_play_redis

    key = "capevaluation.play_code"

    play_code = {
        "name" => "胜平负",
        "items" => ['3','1','0']
    }
    self.redis.hset(key, "001", JSON.generate(play_code))

  end


  def test_003_check_play_items


    play_code = "001"
    items = "3,1"
    self.check_play_items(play_code, items)

    play_code = "099"
    begin
      self.check_play_items(play_code, items)
    rescue => e
      assert_equal(Capevaluation::Exception::InvalidPlayCode, e.class)
    end




    play_code = "001"
    items = "2"
    begin
      self.check_play_items(play_code, items)
    rescue => e
      assert_equal(Capevaluation::Exception::InvalidPlayItem, e.class)
    end

  end





  def config
    @config
  end

  def logger
    @logger
  end

  def redis
    @redis
  end
end